<?php
/**
 * Created by PhpStorm.
 * User: Vito
 * Date: 2022/4/12
 * Time: 10:43
 */

namespace app\api\controller\v1;


use app\api\controller\BaseController;
use app\common\EventName;
use think\facade\Event;
use app\logic\UserLogic;
use app\logic\RepairUserLogic;
use app\service\SmsService;
use app\logic\SmsLogic;
use app\logic\UserOauthLogic;
use app\service\Wechat\WechatH5Service;
use app\service\HttpService;
class User extends BaseController
{

    protected $name = '用户';

    protected $methodName = [
        'detail'        => '详情',
        'getDetail'     => '获取用户详情',
        'SmsTest'       => '发送手机验证码',
        'updateUser'    => '绑定手机号码',
        'sedUserInfo'   => '发送信息到第三方',
        'updateUserInfo'=> '更新用户信息'
    ];

    /**
     * 获取详情(已废弃)
     * @return array
     */
    // public function detail()
    // {
    //     $user = $this->getUserinfo()->scene('api');

    //     Event::trigger(EventName::GET_USER_DETAIL, $user);

    //     $response = [
    //         'detail' => $user,
    //     ];

    //     return success($response);
    // }
    
    /**
     * @Description: 获取用户详情给
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-07 16:20:31
     * @version: 1.0.0
     * @return [type]
     */    
    public function getDetail(){
        $UserLogic = new UserLogic;
        $RepairUserLogic = new RepairUserLogic;
        $params = $this->request->get();
        if(isset($params['id'])){
            $where['id'] = $params['id'];
        }else{
            $where['tel'] = $params['tel'];
            $where['type'] = $params['type'];
        }
        // if($params['type'] == 6){
        //     $res = $RepairUserLogic->findUser($where);
        // }else{
            $res = $UserLogic->findUser($where);
        // }
        

        return success($res,'获取成功');
    }
    /**
     * @Description: 发送手机验证码
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-07 17:48:04
     * @version: 1.0.0
     * @return [type]
     */
    public function sendSms(){
        $params = $this->request->get();
        $result = SmsLogic::send($params['mobile'], null, 'SMS_205275450');
        // print_r(json_decode(json_encode($result),true));
        $result = json_decode(json_encode($result), true);
        $res = true;
        switch ($result['Message']) {
            case 'OK':
                $msssage = '发送成功';
                break;
            case '触发小时级流控Permits:5':
                $res = false;
                $msssage = '频繁操作';
                break;
            case '手机号码格式错误':
                $res = false;
                $msssage = '手机号码格式错误';
                break;    
            default:
                $res = false;
                $msssage = "频繁操作";
                # code...
                break;
        }
        return success($result, $msssage);
    }
    /**
     * @Description: 校验手机验证码
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-07 22:32:33
     * @version: 1.0.0
     * @return [type]
     */
    public function CheckSms(){

        $params = $this->request->get();

        $code = $params['code'];
        
        $moblie = $params['moblie'];
        $res = SmsLogic::check($moblie,$code, 'SMS_205275450');
        
        return success($res,'验证成功');
    }

    /**
     * @Description: 绑定手机号码
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-08 16:31:49
     * @version: 1.0.0
     * @return [type]
     */
    public function updateUser(){
        $UserLogic = new UserLogic;
        $UserOauthLogic = new UserOauthLogic;
        $params = $this->request->post();
        if(!SmsLogic::check($params['tel'], $params['code'], 'SMS_205275450')){
           return error('验证码错误');
        }
        $user_id = $params['user_id'];
        $user = $UserLogic->getModel()::where('tel',$params['tel'])->find();

        $oauth_user_id = $params['user_id'];
        // halt($user);
        if($user){
            $user_id = $user['id'];
        }

        unset($params['user_id'],$params['code']);

        if($params['sex'] == '男'){
            $params['sex'] = 1;
        }else{
            $params['sex'] = 2;
        }
        $params['birthday'] = $params['birthday']/1000;
        $where['id'] = $user_id;
        $res = UserLogic::saveUser($where,$params);
        if($user){
            $UserOauthLogic->getModel()->where('user_id',$oauth_user_id)->update(['user_id'=>$user_id]);
        }
        // if(!$res){
        //     return error('修改失败');
        // }
        $detail = $UserLogic->findUser($where);
        $openidWhere['user_id'] = $user_id;
        $openid = UserOauthLogic::getOpenId($openidWhere);
        $data = [
            'openid'      =>  $openid,
            'mobile'      =>  $params['tel'],
            'username'    =>  $params['real_name'],
            'avatar'      =>  $params['avatar'],
            'updatetime'  =>  time(),
            'createtime'  =>  time()  
        ];
        $this->sedUserInfo($data);
        return success($detail,'成功');

    }
    
    /**
     * @Description: 更新用户信息
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-12-07 15:17:07
     * @version: 1.0.0
     * @return [type]
     */    
    public function updateUserInfo(){

        $UserLogic = new UserLogic;
        $params = $this->request->get();
        $user_id = $params['id'];
        $where['id'] = $user_id;
        $data = [
            'avatar' => $params['avatar']
        ];
        $res = UserLogic::saveUser($where, $data);
        if (!$res) {
            return error('修改失败');
        }
        $detail = $UserLogic->findUser($where);
        return success($detail, '成功');
    }


    /**
     * @Description: 查询用户是关注
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-24 13:45:43
     * @version: 1.0.0
     * @return [type]
     */    
    public function getSubscribe(){

        $params = $this->request->get();
        if(!$params['id']){
            return error('查询失败');
        }
        $where['user_id']   = $params['id'];
        $openId = UserOauthLogic::getOpenId($where);

        $WechatH5 = new WechatH5Service;
        
        $res = $WechatH5->getUserInfo($openId);

        return success($res,'获取成功');
    }

    /**
     * @Description: 调用第三方接口，将用户信息发送过去
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-24 17:54:36
     * @version: 1.0.0
     * @return [type]
     */    
    protected function sedUserInfo($data){
       $http = new HttpService;
       $url  = 'http://expressbox.liuchaoai.com/lockboxapi/addons/lockers/Index/addUserInfo';
    //    $data = [
    //        'openid'      => 'oI2Xm6D2kyxdOXsKlSVUPAz2L8O4',
    //        'mobile'      => '13433397750',
    //        'username'    => '小明'
    //    ];
       $res = $http->senHTTP($url,$data);

        return success($res, '发送成功');
    }
}